package wordit.impl.boardwise;

import wordit.Game;
import wordit.Letter;
import wordit.ea.Generation;
import wordit.ea.Genome;

public class Validator {

	private static Game _game;
	private static boolean _enabled = true;

	public static void setGame(Game game) {
		_game = game;
	}
	
	public static void setEnabled(boolean enabled) {
		_enabled = enabled;
	}
	
	public static void validate(BoardGenome offspring) {
		if (!_enabled) return;
		
		for (Letter l : _game.getLetters()) {
			if (!offspring.containsLetter(l))
				throw new RuntimeException(offspring + " does not contain " + l);
		}
	}

	public static void validate(Generation generation) {
		if (!_enabled) return;
		
		try {
			for (Genome g : generation.getGenomes()) {
				validate((BoardGenome) g);
			}
		} catch (RuntimeException e) {
			System.err.printf("Generation #%d:\n",generation.getCurrGenNumber());
			for (Genome g : generation.getGenomes())
				System.err.println(g);
			throw e;
		}

	}

}
